clear all;
close all;
clc

%build the GUI
%define the plot button
plotbutton=uicontrol('style','pushbutton',...
   'string','Run', ...
   'fontsize',12, ...
   'position',[100,400,50,20], ...
   'callback', 'run=1;');

%define the stop button
erasebutton=uicontrol('style','pushbutton',...
   'string','Stop', ...
   'fontsize',12, ...
   'position',[200,400,50,20], ...
   'callback','freeze=1;');

%define the Quit button
quitbutton=uicontrol('style','pushbutton',...
   'string','Quit', ...
   'fontsize',12, ...
   'position',[300,400,50,20], ...
   'callback','stop=1;close;');

number = uicontrol('style','text', ...
    'string','1', ...
   'fontsize',12, ...
   'position',[20,400,50,20]);
    

n = 128;

a=rand(n,n);

temp=a;

a(temp(:,:)<0.99) = 1; %Healthy percentage 99%
a(temp(:,:)>0.99 & temp(:,:)<0.994) = 2; %Healthy percentage .04%
a(temp(:,:)>0.994 & temp(:,:)<0.998) = 3; %Healthy percentage .04%
a(temp(:,:)>0.998) = 4; %Healthy percentage .02%
imagesc(a);


% % x=2:n-1;
% % y=2:n-1;
% 
x=2:n-1;
y=2:n-1;


stop= 0; %wait for a quit button push
run = 0; %wait for a draw 
freeze = 0; %wait for a freeze
temp=a;
figure
while (stop==0)
    if(run==1)
   
 temp((temp(x,y-1)==2)|(temp(x,y+1)==2)|(temp(x-1,y)==2)|(temp(x+1,y)==2)|(temp(x-1,y-1)==2)...
          |(temp(x-1,y+1)==2)|(temp(3:n,y-1)==2)|(temp(x+1,y+1)==2))=2;
   temp((temp(x,y-1)==3)|(temp(x,y+1)==3)|(temp(x-1,y)==3)|(temp(x+1,y)==3)|(temp(x-1,y-1)==3)...
      |(temp(x-1,y+1)==3)|(temp(3:n,y-1)==3)|(temp(x+1,y+1)==3))=3;
   temp((temp(x,y-1)==4)|(temp(x,y+1)==4)|(temp(x-1,y)==4)|(temp(x+1,y)==4)|(temp(x-1,y-1)==4)...
      |(temp(x-1,y+1)==4)|(temp(3:n,y-1)==4)|(temp(x+1,y+1)==4))=4;
imagesc(temp)



      stepnumber = 1 + str2num(get(number,'string'));
       set(number,'string',num2str(stepnumber))
    
       
    end
   
      if(freeze==1)
        run = 0;
        freeze = 0
      end    
    drawnow;

          

end
    
   

 